//
//  MerchantAllPackageViewController.m
//  UI_HLJ_Frame
//
//  Created by dlios on 15-7-10.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "MerchantAllPackageViewController.h"
#import "MerchantDetailePackageModel.h"
#import "MerchantDetailePackageTableViewCell.h"
#import "PackageDetaileModel.h"

#import "PackageDetailsViewController.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
@interface MerchantAllPackageViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *table;
@property(nonatomic,retain)NSMutableArray *tableViewArr;
@end

@implementation MerchantAllPackageViewController

- (void)dealloc
{
    [_bid release];
    [_table release];
    [_tableViewArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"全部主题";
    self.table= [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49-64) style:UITableViewStylePlain];
    
        self.navigationController.navigationBar.translucent=NO;
    [self.view addSubview:self.table];
    self.table.delegate= self;
    self.table.dataSource =self;
    
    [self createData];
    
    
    // Do any additional setup after loading the view.
}

- (void)createData
{
    self.tableViewArr = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:[NSString stringWithFormat:@"http://hunliji.com/v2/api/app/merchants/%@/works.json?page=1&per_page=20&kind=set_meal", self.bid] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *worksArr = dic[@"works"];
        for (NSDictionary *tempDic in worksArr) {
            MerchantDetailePackageModel *model = [[MerchantDetailePackageModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.tableViewArr addObject:model];
            [model release];
        }

     
        [self.table reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.tableViewArr.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 80;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * reuse= @"reuse";
    MerchantDetailePackageModel *model = self.tableViewArr[indexPath.row];
    MerchantDetailePackageTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[MerchantDetailePackageTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    
//    [cell.merchantImageView sd_setImageWithURL:[NSURL URLWithString:model.cover_path]];
    [cell.merchantImageView sd_setImageWithURL:[NSURL URLWithString:model.cover_path] placeholderImage:[UIImage imageNamed:@"占位图"]];
    cell.title.text=model.title;
    cell.collectors_count.text=[NSString stringWithFormat:@"收藏:%@",model.collectors_count];
    cell.actual_price.text=[NSString stringWithFormat:@"￥%@",model.actual_price];
    cell.actual_price.textColor = [UIColor redColor];
    return cell;



}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PackageDetailsViewController *packageDetailsViewController = [[PackageDetailsViewController alloc] init];
        MerchantDetailePackageModel *model = self.tableViewArr[indexPath.row];
    packageDetailsViewController.idStr = (NSNumber *)model.bid;
    
    
    [self.navigationController pushViewController:packageDetailsViewController animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    [[SDImageCache sharedImageCache] clearMemory];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
